package com.helwen.tools;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 这里用到了递规查询所有节点。`/root/message/a/b` 保存在C:\develop\docs\xml\genarateXmlPath2.txt
 */
public class TestXmlParseTool {

    List<String> totallist=new ArrayList<>();
    //只对元素值处理
    @Test
    public void xmlRecursionParse() throws FileNotFoundException, Exception {

        String parentTemp="message";
        //1.创建Reader对象
        SAXReader reader = new SAXReader();
        //2.加载xml
        Document document = reader.read(new File("src/main/resources/com/template/demo.xml"));
        //3.获取根节点
        Element rootElement = document.getRootElement();
        String name = rootElement.getName();
        boolean b=false;

        Iterator iterator = rootElement.elementIterator();
        File file=new File("C:\\develop\\docs\\xml\\genarateXmlPath2.txt");//输入格式放在这个目录下
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "UTF-8");
        List<String> list=new ArrayList<>();//检查是否有重复的节点。
        Element id = rootElement.element("id");
        System.out.println("id:==="+id.getName());
        //当前节点下的某个亲子节点
        /*Iterator<Element> v_jhmk_pat_transfer = rootElement.elementIterator("V_JHMK_PAT_TRANSFER");
        while (v_jhmk_pat_transfer.hasNext()) {
            Element stu = (Element) v_jhmk_pat_transfer.next();
            String twoLevel = stu.getName();
            System.out.println("========="+twoLevel);
            rootElement.remove(stu);//删除循环节点V_JHMK_PAT_TRANSFER。
        }
        iterator = rootElement.elementIterator();//删除了V_JHMK_PAT_TRANSFER节点后的*/

        getSubElement(name,iterator);//将每个节点路径都放进lis
        StringBuilder sb=new StringBuilder("");
        if(totallist.size()>0){
            for (String s: totallist){
                sb.append("`").append(s).append("`,\r\n");
            }
        }
        osw.write(sb.toString());
        osw.flush();
        osw.close();
        fileOutputStream.close();
    }


    private void getSubElement(String parent,Iterator<Element> iterator){
        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            String twoLevel = stu.getName();
            if(!stu.hasMixedContent()){
                if(!ishasItem(stu.getPath(),totallist)){//对于循环节点，只要一个就可以了。
                    totallist.add(stu.getPath());
                }
            }else{
                Iterator<Element> elementIterator = stu.elementIterator();
                getSubElement(twoLevel,elementIterator);
            }
        }
    }

    private boolean ishasItem(String item,List<String> list){
        boolean b=false;
        if(list.size()>0){
            for(String s:list){
                if(s.equals(item)){
                    b=true;
                    return b;
                }
            }
        }
        return b;
    }
}
